package com.itheima.googleplay9.holder;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.api.Url;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.global.GooglePlayApp;
import com.itheima.googleplay9.global.ImageLoaderOptions;
import com.itheima.googleplay9.ui.activity.ImageScaleActivity;
import com.itheima.googleplay9.util.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by lxj on 2016/5/27.
 */
public class AppScreenHolder extends BaseHolder<AppInfo> {
    @Bind(R.id.ll_screen)
    LinearLayout llScreen;

    @Override
    protected View initHolderView() {
        return View.inflate(GooglePlayApp.context, R.layout.layout_detail_app_screen, null);
    }

    private int width,height;
    private int margin;
    @Override
    public void bindData(AppInfo appInfo) {
        width = Utils.getDimens(R.dimen.dp90);
        height = Utils.getDimens(R.dimen.dp150);
        margin = Utils.getDimens(R.dimen.dp8);

        //截图图片的url集合，数量一般是3-5张
        final ArrayList<String> screen = appInfo.getScreen();
        //遍历集合。往llScreen布局中添加imageView
        for (int i=0;i<screen.size();i++){
            ImageView imageView = new ImageView(GooglePlayApp.context);
            //设置图片的宽高
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
            params.leftMargin = i==0?0:margin;
            imageView.setLayoutParams(params);

            //显示图片
            ImageLoader.getInstance().displayImage(Url.IMAGE_PREFIX+screen.get(i),imageView, ImageLoaderOptions.options);

            llScreen.addView(imageView);

            //添加点击事件开启图片缩放的Activity
            final int temp = i;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(GooglePlayApp.context, ImageScaleActivity.class);
                    //在内存中开启新的任务栈用来存放所要启动的Activity
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putStringArrayListExtra("urlList",screen);
                    //将点击图片的位置传过去
                    intent.putExtra("currentItem", temp);
                    GooglePlayApp.context.startActivity(intent);
                }
            });
        }

    }
}
